//
//  KTVSViewController.m
//  GoShops
//
//  Created by zcj on 15-12-5.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "KTVSViewController.h"
#import "Grouble.h"
#import "TopFilter.h"
#import "MJRefresh.h"
#import "GetUrlString.h"
#import "ShopHomeTableViewCell.h"
#import "SelectFindHttp.h"
#import "MerchantModel.h"
#import "KTVModels.h"
#import "BuyWebViewController.h"
#import "KTVHomeTableViewCell.h"

@interface KTVSViewController ()<UITableViewDataSource,UITableViewDelegate,TopFilterDelegate,UIGestureRecognizerDelegate,MJRefreshBaseViewDelegate>

@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;

@property (nonatomic,strong)UITableView * myTableView;
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic,strong)NSMutableArray * homeKTV;

@end

@implementation KTVSViewController

- (NSMutableArray *)homeKTV
{
    if (_homeKTV == nil) {
        _homeKTV = [[NSMutableArray alloc]init];
    }
    return _homeKTV;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 创建一个tableView，并且创建头
    [self initView];
    [self initNav];
    [self setScrollView];
    [self setRush];
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.myTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.myTableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    [self.foot endRefreshing];
    
}
-(void)loadNewData
{
    [self loadMerchantData];
}
- (void)loadMerchantData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *urlStr = [[GetUrlString sharedManager]urlWithHomeKTV];
        
        //ZCJLog(@"%@",urlStr);
        [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
            //ZCJLog(@"%@",json);
            [self.homeKTV removeAllObjects];
            NSMutableArray * dictArray = json[@"data"];
            for (int i = 0; i < dictArray.count; i ++) {
                KTVModels * ktvModel = [KTVModels objectWithKeyValues:dictArray[i]];
                [self.homeKTV addObject:ktvModel];
            }
            [self.myTableView reloadData];
            [self.head endRefreshing];
            ZCJLog(@"%d",self.homeKTV.count);
        } failture:^(id error) {
            
        }];
    });
}

- (void)initNav
{
    UIButton *MapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    MapBtn.frame = CGRectMake(0, 0, 20, 20);
    [MapBtn setImage:[UIImage imageNamed:@"searchList_btn_delete_6P"] forState:UIControlStateNormal];
    [MapBtn addTarget:self action:@selector(mapBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:MapBtn];
    self.navigationItem.title = @"唱吧";
    
}
- (void)dealloc
{
    [self.foot free];
    [self.head free];
}
- (void)mapBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)initView
{
    //筛选
    UIView *filterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 140)];
    filterView.backgroundColor = [UIColor whiteColor];
    filterView.userInteractionEnabled = YES;
    [self.view addSubview:filterView];
    
    NSArray *filterName = @[@"唱吧",@"附近",@"智能排序",@"帅选"];
    
    //筛选
    for (int i = 0; i < 4; i++) {
        //文字
        UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        filterBtn.frame = CGRectMake(i*ScreenWidth/4, 60, ScreenWidth/4-15, 40);
        filterBtn.tag = 100+i;
        filterBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [filterBtn setTitle:filterName[i] forState:UIControlStateNormal];
        [filterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 设置选中按钮的颜色
        
        [filterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        // 点击按钮
        [filterBtn addTarget:self action:@selector(OnFilterBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            filterBtn.selected = YES;
        }
        [filterView addSubview:filterBtn];
        
        //三角
        UIButton *triangleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        triangleBtn.frame = CGRectMake((i+1)*ScreenWidth/4-15, 76, 8, 7);
        triangleBtn.tag = 120+i;
        [triangleBtn setImage:[UIImage imageNamed:@"icon_arrow_dropdown_normal"] forState:UIControlStateNormal];
        [triangleBtn setImage:[UIImage imageNamed:@"icon_arrow_dropdown_selected"] forState:UIControlStateSelected];
        [filterView addSubview:triangleBtn];
    }
    //搞一个下划线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39.5+60, ScreenWidth, 0.5)];
    lineView.backgroundColor = ZCJColor(192, 192, 192);
    [filterView addSubview:lineView];
    
    // 添加一个tableview
//    self.myTableView = [UITableView initWithTableView:CGRectMake(0, 100, ScreenWidth, ScreenHeight-100) withDelegate:self];
//    [self.view addSubview:self.myTableView];
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, ScreenWidth, ScreenHeight-100)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];

    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 70, ScreenWidth, ScreenHeight)];
    self.imageView.image = [UIImage imageNamed:@"Default"];
    self.imageView.hidden = YES;
    [self.view addSubview:self.imageView];
    
}

// 搞一个ScrollView可以点击"全部分类",@"全城",@"智能排序可以滚动
- (void)setScrollView
{
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, ScreenWidth, ScreenHeight-150)];
    _maskView.backgroundColor = RGBA(0, 0, 0, 0.5);
    [self.view addSubview:_maskView];
    _maskView.hidden = YES;
    
//    //添加手势,需要实现
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapMaskView:)];
//    tap.delegate = self;
//    [_maskView addGestureRecognizer:tap];
    // 设置这个view的宽高
    TopFilter *filterView =  [[TopFilter alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight- 250)];
    filterView.delegate = self;
    [_maskView addSubview:filterView];
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _maskView.hidden = YES;
}
- (void)OnFilterBtn:(UIButton *)btn
{
    for (int i = 0; i < 3; i++) {
        UIButton *menubtn = (UIButton *)[self.view viewWithTag:100+i];
        UIButton *arrowBtn = (UIButton *)[self.view viewWithTag:120+i];
        menubtn.selected = NO;
        arrowBtn.selected = NO;
    }
    btn.selected = YES;
    UIButton *sjBtn = (UIButton *)[self.view viewWithTag:btn.tag+20];
    sjBtn.selected = YES;
    _maskView.hidden = NO;
    
}
#pragma mark - 点击了按钮
- (void)OnTapMaskView:(UIGestureRecognizer *)gester
{
    _maskView.hidden = YES;
}
#pragma mark - 点击了下拉菜单中的某个cell才执行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath withId:(NSNumber *)ID
{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.homeKTV.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KTVHomeTableViewCell * cell = [KTVHomeTableViewCell KTVHomeTableViewCell:tableView];
    cell.ktvModels = self.homeKTV[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KTVModels * ktvModel = self.homeKTV[indexPath.row];
    if (ktvModel.introduction.length == 0) {
        return 120;
    }else{
        return 140;
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuyWebViewController * webView = [[BuyWebViewController alloc]init];
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
